#ifndef _FOREST_LOADER_
#define _FOREST_LOADER_

#include "FEngineType.h"
#include "Node.h"
#include "PagedEntity.h"
#include <vector>

//#define _FOREST_LOADER_DEBUG_

namespace FEngine
{
	class PagedTreeBlock;

	/**
	 * Handle the pagination of one 3D model.
	 * One instance of the object is first created,
	 * then each instance of the object is set and render.
	 */
	class ForestLoader
	{

		public:

			/**
			 * Constructor.
			 * @param _blockSize Size of one page.
			 * @param _numberBlock Number of page.
			 * @param _position Position of the first page.
			 * @param _maxViewDistance View range of the object inside the block.
			 * @param _impostorApparitionDistance View range from wich the impostor are drawn.
			 */
			ForestLoader(int _blockSize, int _numberBlock, Vector2 _pos, float _maxViewDistance, float _impostorApparitionDistance);
			~ForestLoader();
			
			/**
			 * Create the instance of the object to replicate.
			 * @param _fileName Name of the .x file of the mesh.
			 * @param _textureName Name of the texture file of the mesh.
			 */
			void createObject(const std::string& _fileName, const std::string& _textureName);
			
			/**
			 * Add one instance of the object to the scene.
			 * @param _pos Position of the object.
			 * @param _rot Position of the object.
			 * @param _sca Position of the object.
			 */
			void addObject(const Vector3& _pos, const Vector3& _rot, const Vector3& _sca);

			/**
			 * Add one instance of the object to the scene.
			 * @param _worldMat World matrix of the object.
			 */
			void addObject(const Matrix4& _worldMat);
			
			int	getNumberPage() const;
			int	getNumberVisiblePage() const;
			
			int	getNumberObject() const;
			int	getNumberVisibleObject() const;
			int	getNumberImpostor() const;
			int	getNumberVisibleImpostor() const;
			
			void update(double _elapsedTime);
			void render(double _elapsedTime);


		private:
			
			LPDIRECT3DVERTEXBUFFER9			mVertexBufferImpostor;
			IDirect3DVertexDeclaration9*	mVertexDeclarationImpostor;
			IDirect3DVertexDeclaration9*	mVertexDeclarationObject;
			LPDIRECT3DINDEXBUFFER9			mIndexBufferImpostor;
			
			int								mNumberVerticesImpostor;
			int								mNumberPrimitivesImpostor;

			int								mBlockSize;
			int								mNumberBlock;
			Vector2							mPosition;
			Vector2							mSize;
			float							mMaximumViewDistance;
			float							mImpostorApparitionDistance;

			std::vector<PagedTreeBlock*>	mPagedBlockVector;

			PagedEntity*					mPagedObject;	// Our object to replicate
			
			void _renderObject(double _elapsedTime);
			void _renderImpostor(double _elapsedTime);
			
			void _setImpostorBuffers();

	};

} // namespace FEngine

#endif
